package com.mySampleApplication.client.codeRule;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mySampleApplication.client.CodeRuleAtter.CodeRuleAtter;
import com.mySampleApplication.client.CodeRuleAtter.CodeRuleAtterDto;
import com.mySampleApplication.client.CodeRuleAtter.CodeRuleAtterService;
import com.mySampleApplication.client.components.DataElementCategoryDataSource;
import com.mySampleApplication.server.Encoder;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VLayout;

import java.util.*;

/**
 * 用法
 * // 创建按钮和事件处理
 * Button addButton = new Button("Add");
 * addButton.setShowRollOver(true);
 * addButton.setShowDown(true);
 * addButton.addClickHandler(event -> showAddDialog(listGrid));
 * private static void showAddDialog(ListGrid listGrid) {
 * Window dialog=SShowWindow.getShowWindow();
 * dialog.show();
 * }
 */
public class CodeRuleWindow {
    public static Window getShowWindow(int status, int width, int height, String title, final ListGridRecord record, ListGrid listGrid, boolean istrue, String id, String name, String bmgf, String type,List<ListGridRecord> fdrecordList) {
        final Window winModal = new Window();
        //winModal.setAutoSize(true);
        winModal.setWidth(width);
        winModal.setHeight(height);
        winModal.setTitle(title);
        winModal.setShowMinimizeButton(false);
        winModal.setIsModal(true);
        winModal.setShowModalMask(true);
        winModal.setCanDragReposition(true);
        winModal.setCanDragResize(true);
        winModal.centerInPage();
        winModal.addCloseClickHandler(new CloseClickHandler() {
            public void onCloseClick(CloseClickEvent event) {
                winModal.destroy();
            }
        });
        DynamicForm form = new DynamicForm();
        form.setPadding(20);
        form.setWidth100();
        form.setHeight(200);
        form.setNumCols(5);
        form.setCellPadding(10);

        TextItem bmid = new TextItem("id", "编码id");
        if(!id.equals("")){
            bmid.setValue(getuuid(32));
        }
        TextItem db_model_idField = new TextItem("db_model_id", "数据模型id");
        db_model_idField.setValue(id);
        TextItem db_model_name = new TextItem("db_model_name", "数据模型");
        if(!name.equals("")){
            db_model_name.setValue(name);
        }
        db_model_name.disable();
        TextItem mcField = new TextItem("mc", "规则名称");
        mcField.setRequired(true);
        TextItem msField = new TextItem("ms", "描述");
        TextItem fbzt = new TextItem("fbzt", "发布状态");
        fbzt.setValue("未发布");
        TextItem fgfField = new TextItem("fgf", "分隔符");
        TextItem qzField = new TextItem("qz", "前缀");
        TextItem hzField = new TextItem("hz", "后缀");
        TextItem bbField = new TextItem("bb", "版本");
        TextItem bmgfField = new TextItem("bmgf", "编码规范");
        TextItem statusField = new TextItem("status", "使用状态");
        statusField.setValue("add");
        bmgfField.setValue(bmgf);

        bbField.setValue(1);
        bbField.disable();

        if (record != null) {
            // 编辑模式下，将已有数据显示在表单中
            bmid.setValue(record.getAttribute("id"));
            db_model_idField.setValue(record.getAttribute("db_model_id"));
            db_model_name.setValue(record.getAttribute("db_model_name"));
            fbzt.setValue(record.getAttribute("fbzt"));
            mcField.setValue(record.getAttribute("mc"));
            msField.setValue(record.getAttribute("ms"));
            fgfField.setValue(record.getAttribute("fgf"));
            qzField.setValue(record.getAttribute("qz"));
            hzField.setValue(record.getAttribute("hz"));
            bmgfField.setValue(record.getAttribute("bmgf"));
            statusField.setValue(record.getAttribute("status"));
            // 禁用表单
            if (!type.equals("bj")) {
                form.setDisabled(true);
            }

            if (status == 0) {
                bbField.setValue(record.getAttribute("bb"));
            } else {
                bbField.setValue(Integer.parseInt(record.getAttribute("bb")) + 1);
            }
        }
        ListGridRecord re = new ListGridRecord();
        re.setAttribute("db_model_id", db_model_idField.getValueAsString());
        re.setAttribute("id", bmid.getValueAsString());
        re.setAttribute("bb", bbField.getValueAsString());
        re.setAttribute("type", type);

        Map<String, Object> listMap = CodeRuleAtter.getListGrid(re);
        ListGrid listGridAtter = (ListGrid) listMap.get("listGrid");
        ListGrid finalListGridAtter = (ListGrid) listMap.get("finalListGrid");
        List<ListGridRecord> recordList = new ArrayList<>();
        Map<String, String> mapPageAtter = new HashMap<>();
        mapPageAtter.put("pageSize", "1");
        mapPageAtter.put("pageNumber", "10");
        Map<String, String> mapf = new HashMap<>();
        if (type.equals("xq")) {
            mapf.put("coderule_id", re.getAttribute("id"));
            CodeRuleAtterService.App.getInstance().getSelect(mapf, mapPageAtter, new AsyncCallback<CodeRuleAtterDto>() {
                public void onSuccess(CodeRuleAtterDto result) {
                    List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                    for (Map<String, String> data : keyValueMap) {
                        ListGridRecord record = new ListGridRecord();
                        data.forEach((k, v) -> {
                            record.setAttribute(k, v);
                        });
                        // 设置其他需要的属性
                        recordList.add(record);
                    }
                    listGridAtter.setData(recordList.toArray(new ListGridRecord[0]));
                }

                public void onFailure(Throwable caught) {
                    SC.warn("获取数据失败！");
                }
            });
        }else {
            if (type.equals("bj")) {
                if(fdrecordList!=null){
                    listGridAtter.setData(fdrecordList.toArray(new ListGridRecord[0]));
                }else{
                    mapf.put("coderule_id", re.getAttribute("id"));
                    CodeRuleAtterService.App.getInstance().getSelect(mapf, mapPageAtter, new AsyncCallback<CodeRuleAtterDto>() {
                        public void onSuccess(CodeRuleAtterDto result) {
                            List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                            for (Map<String, String> data : keyValueMap) {
                                ListGridRecord record = new ListGridRecord();
                                data.forEach((k, v) -> {
                                    record.setAttribute(k, v);
                                });
                                // 设置其他需要的属性
                                recordList.add(record);
                            }
                            if(recordList.size()<1 & fdrecordList!=null){
                                listGridAtter.setData(fdrecordList.toArray(new ListGridRecord[0]));
                            }else{
                                listGridAtter.setData(recordList.toArray(new ListGridRecord[0]));
                            }

                        }
                        public void onFailure(Throwable caught) {
                            SC.warn("获取数据失败！");
                        }
                    });
                }


            }else{
                if(fdrecordList!=null){
                    listGridAtter.setData(fdrecordList.toArray(new ListGridRecord[0]));
                }
            }
        }
        // 设置数据源到列表网格
//         listGridAtter.setDataSource(dataSourceAtter);
        // 加载数据
        listGridAtter.fetchData();
        VLayout layout = new VLayout();
        // 组装页面
        //layout.addMember(searchArea);
        layout.addMember((HLayout) listMap.get("buttonLayout"));
        // 添加列表网格到布局容器中
        layout.addMember(listGridAtter);

        Button saveButton = new Button("保存");
        saveButton.setVisible(false);
        saveButton.addClickHandler(event -> {
            RecordList r = finalListGridAtter.getDataAsRecordList();

//            if(r.getLength()<1){
//                SC.say( "请编辑分段配置！");
//            }else{
            if (form.validate()) {

//                CodeRuleService.App.getInstance().selectById(bmid.getValueAsString(), new AsyncCallback<Map<String, String>>() {
//                    public void onSuccess(Map<String, String> result) {
                        if(!statusField.getValueAsString().equals("add")){

                            CodeRuleAtterService.App.getInstance().deleteByCoderuleId(bmid.getValueAsString(), new AsyncCallback<String>() {
                                public void onSuccess(String result) {
                                    for (int i = 0; i < r.getLength(); i++) {
                                        LinkedHashMap<String, String> map = new LinkedHashMap<>();
                                        map.put("fdmc", r.get(i).getAttribute("fdmc"));
                                        map.put("fdsx", r.get(i).getAttribute("fdsx"));
                                        map.put("fdlx", r.get(i).getAttribute("fdlx"));
                                        map.put("zdsxm", r.get(i).getAttribute("zdsxm"));
                                        map.put("gdz", r.get(i).getAttribute("gdz"));
                                        map.put("lshcd", r.get(i).getAttribute("lshcd"));
                                        map.put("lshbc", r.get(i).getAttribute("lshbc"));
                                        map.put("lshgs", r.get(i).getAttribute("lshgs"));
                                        map.put("coderule_id", bmid.getValueAsString());
                                        map.put("bb", bbField.getValueAsString());
                                        CodeRuleAtterService.App.getInstance().setInsert(map, new AsyncCallback<String>() {
                                            public void onSuccess(String result) {

                                            }

                                            public void onFailure(Throwable caught) {
                                                SC.warn("新增数据失败！");
                                            }
                                        });
                                    }
                                }

                                public void onFailure(Throwable caught) {
                                    SC.warn("新增数据失败！");
                                }
                            });

                            // 更新已有记录
                            record.setAttribute("id", bmid.getValueAsString());
                            record.setAttribute("db_model_id", db_model_idField.getValueAsString());
                            record.setAttribute("db_model_name", db_model_name.getValueAsString());
                            record.setAttribute("mc", mcField.getValueAsString());
                            record.setAttribute("ms", msField.getValueAsString());
                            record.setAttribute("fgf", fgfField.getValueAsString());
                            record.setAttribute("qz", qzField.getValueAsString());
                            record.setAttribute("hz", hzField.getValueAsString());
                            record.setAttribute("bb", bbField.getValueAsString());
                            record.setAttribute("fbzt", fbzt.getValueAsString());
                            record.setAttribute("bmgf", bmgfField.getValueAsString());
//                    listGrid.updateData(record);
                            Map<String, String> map = new HashMap<>();
                            map.put("id", record.getAttribute("id"));
                            map.put("db_model_id", db_model_idField.getValueAsString());
                            map.put("db_model_name", db_model_name.getValueAsString());
                            map.put("mc", mcField.getValueAsString());
                            map.put("ms", msField.getValueAsString());
                            map.put("fgf", fgfField.getValueAsString());
                            map.put("qz", qzField.getValueAsString());
                            map.put("hz", hzField.getValueAsString());
                            map.put("bb", bbField.getValueAsString());
                            map.put("fbzt", fbzt.getValueAsString());
                            map.put("bmgf", bmgfField.getValueAsString());
                            CodeRuleService.App.getInstance().setUpdate(map, new AsyncCallback<String>() {
                                public void onSuccess(String result) {
                                    Map<String, String> mapPage = new HashMap<>();
                                    mapPage.put("pageSize", "1");
                                    mapPage.put("pageNumber", "10");
                                    List<ListGridRecord> recordList = new ArrayList<>();
                                    CodeRuleService.App.getInstance().getSelect(new HashMap<>(), mapPage, new AsyncCallback<CodeRuleDto>() {
                                        public void onSuccess(CodeRuleDto result) {
                                            List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                                            for (Map<String, String> data : keyValueMap) {
                                                ListGridRecord record = new ListGridRecord();
                                                data.forEach((k, v) -> {
                                                    record.setAttribute(k, v);
                                                });
                                                // 设置其他需要的属性
                                                recordList.add(record);
                                            }
                                            listGrid.setData(recordList.toArray(new ListGridRecord[0]));
                                            SC.say("修改数据成功！");
                                            //list.addAll(result);
                                        }

                                        public void onFailure(Throwable caught) {
                                            SC.warn("获取数据失败！");
                                        }
                                    });
                                }

                                public void onFailure(Throwable caught) {
                                    SC.warn("RPC failed");
                                }
                            });
                            listGrid.updateData(record);
                        }else{

                            for (int i = 0; i < r.getLength(); i++) {
                                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                                map.put("fdmc", r.get(i).getAttribute("fdmc"));
                                map.put("fdsx", r.get(i).getAttribute("fdsx"));
                                map.put("fdlx", r.get(i).getAttribute("fdlx"));
                                map.put("zdsxm", r.get(i).getAttribute("zdsxm"));
                                map.put("gdz", r.get(i).getAttribute("gdz"));
                                map.put("lshcd", r.get(i).getAttribute("lshcd"));
                                map.put("lshbc", r.get(i).getAttribute("lshbc"));
                                map.put("lshgs", r.get(i).getAttribute("lshgs"));
                                map.put("coderule_id", bmid.getValueAsString());
                                map.put("bb", bbField.getValueAsString());

                                CodeRuleAtterService.App.getInstance().setInsert(map, new AsyncCallback<String>() {
                                    public void onSuccess(String result) {

                                    }

                                    public void onFailure(Throwable caught) {
                                        SC.warn("新增数据失败！");
                                    }
                                });
                            }

//                    SC.say(+"");
                            // 添加新记录
                            ListGridRecord newRecord = new ListGridRecord();
                            newRecord.setAttribute("id", bmid.getValueAsString());
                            newRecord.setAttribute("db_model_id", db_model_idField.getValueAsString());
                            newRecord.setAttribute("db_model_name", db_model_name.getValueAsString());
                            newRecord.setAttribute("mc", db_model_idField.getDisplayValue() + "编码规则");
                            newRecord.setAttribute("ms", msField.getValueAsString());
                            newRecord.setAttribute("fgf", fgfField.getValueAsString());
                            newRecord.setAttribute("qz", qzField.getValueAsString());
                            newRecord.setAttribute("hz", hzField.getValueAsString());
                            newRecord.setAttribute("status", "禁用");
                            newRecord.setAttribute("bb", bbField.getValueAsString());
                            newRecord.setAttribute("fbzt", fbzt.getValueAsString());
                            newRecord.setAttribute("bmgf", bmgfField.getValueAsString());

                            Map<String, String> map = new HashMap<>();
                            map.put("id", bmid.getValueAsString());
                            map.put("db_model_id", db_model_idField.getValueAsString());
                            map.put("db_model_name", db_model_name.getValueAsString());
                            map.put("mc", db_model_name.getDisplayValue() + "编码规则");
                            map.put("ms", msField.getValueAsString());
                            map.put("fgf", fgfField.getValueAsString());
                            map.put("qz", qzField.getValueAsString());
                            map.put("hz", hzField.getValueAsString());
                            map.put("status", "禁用");
                            map.put("bb", bbField.getValueAsString());
                            map.put("fbzt", "未发布");
                            map.put("bmgf", bmgfField.getValueAsString());
                            Map<String, String> mapPage = new HashMap<>();
                            mapPage.put("pageSize", "1");
                            mapPage.put("pageNumber", "10");
                            Map<String, String> parameter = new HashMap<>();
                            parameter.put("db_model_id", db_model_idField.getValueAsString());
                            CodeRuleService.App.getInstance().setInsert(map, new AsyncCallback<String>() {
                                public void onSuccess(String result) {
                                    List<ListGridRecord> recordList = new ArrayList<>();
                                    CodeRuleService.App.getInstance().getSelect(new HashMap<>(), mapPage, new AsyncCallback<CodeRuleDto>() {
                                        public void onSuccess(CodeRuleDto result) {
                                            List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                                            for (Map<String, String> data : keyValueMap) {
                                                ListGridRecord record = new ListGridRecord();
                                                data.forEach((k, v) -> {
                                                    record.setAttribute(k, v);
                                                });
                                                // 设置其他需要的属性
                                                recordList.add(record);
                                            }
                                            listGrid.setData(recordList.toArray(new ListGridRecord[0]));
                                            SC.say("新增数据成功！");
                                        }

                                        public void onFailure(Throwable caught) {
                                            SC.warn("获取数据失败！");
                                        }
                                    });
                                }
                                public void onFailure(Throwable caught) {
                                    SC.warn("RPC failed");
                                }
                            });

                        }
//                    }
//                    public void onFailure(Throwable caught) {
//                        SC.warn("获取数据失败！");
//                    }
//                });

            }
            winModal.hide();
//            }
        });
        Button fbButton = new Button("发布");
//        fbButton.disable();
        fbButton.setVisible(false);
        fbButton.addClickHandler(event -> {
            RecordList r = finalListGridAtter.getDataAsRecordList();
            if (r.getLength() < 1) {
                SC.say("请编辑分段配置！");
            } else {
                if (form.validate()) {
                    if (record != null) {
                        CodeRuleAtterService.App.getInstance().deleteByCoderuleId(bmid.getValueAsString(), new AsyncCallback<String>() {
                            public void onSuccess(String result) {
                                for (int i = 0; i < r.getLength(); i++) {
                                    LinkedHashMap<String, String> map = new LinkedHashMap<>();
                                    map.put("fdmc", r.get(i).getAttribute("fdmc"));
                                    map.put("fdsx", r.get(i).getAttribute("fdsx"));
                                    map.put("fdlx", r.get(i).getAttribute("fdlx"));
                                    map.put("zdsxm", r.get(i).getAttribute("zdsxm"));
                                    map.put("gdz", r.get(i).getAttribute("gdz"));
                                    map.put("lshcd", r.get(i).getAttribute("lshcd"));
                                    map.put("lshbc", r.get(i).getAttribute("lshbc"));
                                    map.put("lshgs", r.get(i).getAttribute("lshgs"));
                                    map.put("coderule_id", bmid.getValueAsString());
                                    map.put("bb", bbField.getValueAsString());
                                    CodeRuleAtterService.App.getInstance().setInsert(map, new AsyncCallback<String>() {
                                        public void onSuccess(String result) {

                                        }

                                        public void onFailure(Throwable caught) {
                                            SC.warn("新增数据失败！");
                                        }
                                    });
                                }
                            }

                            public void onFailure(Throwable caught) {
                                SC.warn("新增数据失败！");
                            }
                        });

                        // 更新已有记录
                        record.setAttribute("id", bmid.getValueAsString());
                        record.setAttribute("db_model_id", db_model_idField.getValueAsString());
                        record.setAttribute("db_model_name", db_model_name.getValueAsString());
                        record.setAttribute("mc", mcField.getValueAsString());
                        record.setAttribute("ms", msField.getValueAsString());
                        record.setAttribute("fgf", fgfField.getValueAsString());
                        record.setAttribute("qz", qzField.getValueAsString());
                        record.setAttribute("hz", hzField.getValueAsString());
                        record.setAttribute("bb", bbField.getValueAsString());
                        record.setAttribute("bmgf", bmgfField.getValueAsString());
                        record.setAttribute("fbzt", "发布");
//                    listGrid.updateData(record);
                        Map<String, String> map = new HashMap<>();
                        map.put("id", record.getAttribute("id"));
                        map.put("db_model_id", db_model_idField.getValueAsString());
                        map.put("db_model_name", db_model_name.getValueAsString());
                        map.put("mc", mcField.getValueAsString());
                        map.put("ms", msField.getValueAsString());
                        map.put("fgf", fgfField.getValueAsString());
                        map.put("qz", qzField.getValueAsString());
                        map.put("hz", hzField.getValueAsString());
                        map.put("bb", bbField.getValueAsString());
                        map.put("bmgf", bmgfField.getValueAsString());
                        map.put("fbzt", "发布");
                        CodeRuleService.App.getInstance().setUpdate(map, new AsyncCallback<String>() {
                            public void onSuccess(String result) {
                                Map<String, String> mapPage = new HashMap<>();
                                mapPage.put("pageSize", "1");
                                mapPage.put("pageNumber", "10");
                                List<ListGridRecord> recordList = new ArrayList<>();
                                CodeRuleService.App.getInstance().getSelect(new HashMap<>(), mapPage, new AsyncCallback<CodeRuleDto>() {
                                    public void onSuccess(CodeRuleDto result) {
                                        List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                                        for (Map<String, String> data : keyValueMap) {
                                            ListGridRecord record = new ListGridRecord();
                                            data.forEach((k, v) -> {
                                                record.setAttribute(k, v);
                                            });
                                            // 设置其他需要的属性
                                            recordList.add(record);
                                        }
                                        listGrid.setData(recordList.toArray(new ListGridRecord[0]));
                                        SC.say("修改数据成功！");
                                        //list.addAll(result);
                                    }

                                    public void onFailure(Throwable caught) {
                                        SC.warn("获取数据失败！");
                                    }
                                });
                            }

                            public void onFailure(Throwable caught) {
                                SC.warn("RPC failed");
                            }
                        });
                        listGrid.updateData(record);
                    } else {
                        for (int i = 0; i < r.getLength(); i++) {
                            LinkedHashMap<String, String> map = new LinkedHashMap<>();
                            map.put("fdmc", r.get(i).getAttribute("fdmc"));
                            map.put("fdsx", r.get(i).getAttribute("fdsx"));
                            map.put("fdlx", r.get(i).getAttribute("fdlx"));
                            map.put("zdsxm", r.get(i).getAttribute("zdsxm"));
                            map.put("gdz", r.get(i).getAttribute("gdz"));
                            map.put("lshcd", r.get(i).getAttribute("lshcd"));
                            map.put("lshbc", r.get(i).getAttribute("lshbc"));
                            map.put("lshgs", r.get(i).getAttribute("lshgs"));
                            map.put("coderule_id", bmid.getValueAsString());
                            map.put("bb", bbField.getValueAsString());
                            CodeRuleAtterService.App.getInstance().setInsert(map, new AsyncCallback<String>() {
                                public void onSuccess(String result) {

                                }

                                public void onFailure(Throwable caught) {
                                    SC.warn("新增数据失败！");
                                }
                            });
                        }

                        // 添加新记录
                        ListGridRecord newRecord = new ListGridRecord();
                        newRecord.setAttribute("id", bmid.getValueAsString());
                        newRecord.setAttribute("db_model_id", db_model_idField.getValueAsString());
                        newRecord.setAttribute("db_model_name", db_model_name.getValueAsString());
                        newRecord.setAttribute("mc", db_model_idField.getDisplayValue() + "编码规则");
                        newRecord.setAttribute("ms", msField.getValueAsString());
                        newRecord.setAttribute("fgf", fgfField.getValueAsString());
                        newRecord.setAttribute("qz", qzField.getValueAsString());
                        newRecord.setAttribute("hz", hzField.getValueAsString());
                        newRecord.setAttribute("status", "禁用");
                        newRecord.setAttribute("bb", bbField.getValueAsString());
                        newRecord.setAttribute("bmgf", bmgfField.getValueAsString());
                        newRecord.setAttribute("fbzt", "发布");

                        Map<String, String> map = new HashMap<>();
                        map.put("id", bmid.getValueAsString());
                        map.put("db_model_id", db_model_idField.getValueAsString());
                        map.put("db_model_name", db_model_name.getValueAsString());
                        map.put("mc", db_model_name.getDisplayValue() + "编码规则");
                        map.put("ms", msField.getValueAsString());
                        map.put("fgf", fgfField.getValueAsString());
                        map.put("qz", qzField.getValueAsString());
                        map.put("hz", hzField.getValueAsString());
                        map.put("status", "禁用");
                        map.put("bb", bbField.getValueAsString());
                        map.put("bmgf", bmgfField.getValueAsString());
                        map.put("fbzt", "发布");
                        Map<String, String> mapPage = new HashMap<>();
                        mapPage.put("pageSize", "1");
                        mapPage.put("pageNumber", "10");
                        Map<String, String> parameter = new HashMap<>();
                        parameter.put("db_model_id", db_model_idField.getValueAsString());
                        CodeRuleService.App.getInstance().setInsert(map, new AsyncCallback<String>() {
                            public void onSuccess(String result) {
                                List<ListGridRecord> recordList = new ArrayList<>();
                                CodeRuleService.App.getInstance().getSelect(new HashMap<>(), mapPage, new AsyncCallback<CodeRuleDto>() {
                                    public void onSuccess(CodeRuleDto result) {
                                        List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                                        for (Map<String, String> data : keyValueMap) {
                                            ListGridRecord record = new ListGridRecord();
                                            data.forEach((k, v) -> {
                                                record.setAttribute(k, v);
                                            });
                                            // 设置其他需要的属性
                                            recordList.add(record);
                                        }
                                        listGrid.setData(recordList.toArray(new ListGridRecord[0]));
                                        SC.say("新增数据成功！");
                                    }

                                    public void onFailure(Throwable caught) {
                                        SC.warn("获取数据失败！");
                                    }
                                });
                            }

                            public void onFailure(Throwable caught) {
                                SC.warn("RPC failed");
                            }
                        });
                    }
                }
                winModal.hide();
            }
        });

        Button sbButton = new Button("升版");
        sbButton.setVisible(false);
//        sbButton.hide();
        sbButton.addClickHandler(event -> {
            RecordList r = finalListGridAtter.getDataAsRecordList();
            if (r.getLength() < 1) {
                SC.say("请编辑分段配置！");
            } else {
                //获取当前版本的编码规则数据
                CodeRuleService.App.getInstance().selectById(bmid.getValueAsString(), new AsyncCallback<Map<String, String>>() {
                    public void onSuccess(Map<String, String> codeRuleResult) {
                        Map<String, String> map = new HashMap<>();
                        map.put("id", codeRuleResult.get("id"));
                        map.put("db_model_id", codeRuleResult.get("db_model_id"));
                        map.put("db_model_name", codeRuleResult.get("db_model_name"));
                        map.put("mc", codeRuleResult.get("mc"));
                        map.put("ms", codeRuleResult.get("ms"));
                        map.put("fgf", codeRuleResult.get("fgf"));
                        map.put("qz", codeRuleResult.get("qz"));
                        map.put("hz", codeRuleResult.get("hz"));
                        map.put("bb", codeRuleResult.get("bb"));
                        map.put("bmgf", codeRuleResult.get("bmgf"));
                        map.put("fbzt", codeRuleResult.get("fbzt"));
                        map.put("status", codeRuleResult.get("status"));
//                        CodeRuleServlet.setSbInsert(map);
                        CodeRuleService.App.getInstance().setSbInsert(map, new AsyncCallback<String>() {
                            public void onSuccess(String result) {
                                CodeRuleService.App.getInstance().setSbUpdate(map, new AsyncCallback<String>() {
                                    public void onSuccess(String result) {

                                    }

                                    public void onFailure(Throwable caught) {
                                        SC.warn("RPC failed");
                                    }
                                });
                            }

                            public void onFailure(Throwable caught) {
                                SC.warn("RPC failed");
                            }
                        });
                    }

                    public void onFailure(Throwable caught) {
                        SC.warn("获取数据失败！");
                    }
                });

                CodeRuleAtterService.App.getInstance().deleteByCoderuleId(bmid.getValueAsString(), new AsyncCallback<String>() {
                    public void onSuccess(String result) {
                        for (int i = 0; i < r.getLength(); i++) {
                            LinkedHashMap<String, String> map = new LinkedHashMap<>();
                            map.put("fdmc", r.get(i).getAttribute("fdmc"));
                            map.put("fdsx", r.get(i).getAttribute("fdsx"));
                            map.put("fdlx", r.get(i).getAttribute("fdlx"));
                            map.put("zdsxm", r.get(i).getAttribute("zdsxm"));
                            map.put("gdz", r.get(i).getAttribute("gdz"));
                            map.put("lshcd", r.get(i).getAttribute("lshcd"));
                            map.put("lshbc", r.get(i).getAttribute("lshbc"));
                            map.put("lshgs", r.get(i).getAttribute("lshgs"));
                            map.put("coderule_id", bmid.getValueAsString());
                            map.put("bb", (Integer.parseInt(bbField.getValueAsString()) + 1) + "");
                            CodeRuleAtterService.App.getInstance().setInsert(map, new AsyncCallback<String>() {
                                public void onSuccess(String result) {

                                }

                                public void onFailure(Throwable caught) {
                                    SC.warn("新增数据失败！");
                                }
                            });
                        }
                    }

                    public void onFailure(Throwable caught) {
                        SC.warn("新增数据失败！");
                    }
                });

                // 更新已有记录
                record.setAttribute("id", bmid.getValueAsString());
                record.setAttribute("db_model_id", db_model_idField.getValueAsString());
                record.setAttribute("db_model_name", db_model_name.getValueAsString());
                record.setAttribute("mc", mcField.getValueAsString());
                record.setAttribute("ms", msField.getValueAsString());
                record.setAttribute("fgf", fgfField.getValueAsString());
                record.setAttribute("qz", qzField.getValueAsString());
                record.setAttribute("hz", hzField.getValueAsString());
                record.setAttribute("status", "禁用");
                record.setAttribute("bb", (Integer.parseInt(bbField.getValueAsString()) + 1) + "");
                record.setAttribute("bmgf", bmgfField.getValueAsString());
//                    listGrid.updateData(record);
                Map<String, String> map = new HashMap<>();
                map.put("id", record.getAttribute("id"));
                map.put("db_model_id", db_model_idField.getValueAsString());
                map.put("db_model_name", db_model_name.getValueAsString());
                map.put("mc", mcField.getValueAsString());
                map.put("ms", msField.getValueAsString());
                map.put("fgf", fgfField.getValueAsString());
                map.put("qz", qzField.getValueAsString());
                map.put("hz", hzField.getValueAsString());
                map.put("status", "禁用");
                map.put("bb", (Integer.parseInt(bbField.getValueAsString()) + 1) + "");
                map.put("bmgf", bmgfField.getValueAsString());
                CodeRuleService.App.getInstance().setUpdate(map, new AsyncCallback<String>() {
                    public void onSuccess(String result) {
                        Map<String, String> mapPage = new HashMap<>();
                        mapPage.put("pageSize", "1");
                        mapPage.put("pageNumber", "10");
                        List<ListGridRecord> recordList = new ArrayList<>();
                        CodeRuleService.App.getInstance().getSelect(new HashMap<>(), mapPage, new AsyncCallback<CodeRuleDto>() {
                            public void onSuccess(CodeRuleDto result) {
                                List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                                for (Map<String, String> data : keyValueMap) {
                                    ListGridRecord record = new ListGridRecord();
                                    data.forEach((k, v) -> {
                                        record.setAttribute(k, v);
                                    });
                                    // 设置其他需要的属性
                                    recordList.add(record);

                                }
                                listGrid.setData(recordList.toArray(new ListGridRecord[0]));
                                SC.say("修改数据成功！");
                                //list.addAll(result);
                            }

                            public void onFailure(Throwable caught) {
                                SC.warn("获取数据失败！");
                            }
                        });
                    }

                    public void onFailure(Throwable caught) {
                        SC.warn("RPC failed");
                    }
                });

                listGrid.updateData(record);
                winModal.hide();
            }
        });

        IButton hqbmImg = new IButton();
        hqbmImg.setTitle("测试编码");
        hqbmImg.setVisible(false);
//        hqbmImg.setWidth(60);
//                    editImg.setRight(10);
        hqbmImg.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                RecordList r = finalListGridAtter.getDataAsRecordList();
                if (r.getLength() < 1) {
                    SC.say("请编辑分段配置！");
                } else {
                    List<Map<String, String>> listMap = new LinkedList<>();
                    for (int i = 0; i < r.getLength(); i++) {
                        Map<String, String> map=new HashMap<>();
                        map.put("fdmc", r.get(i).getAttribute("fdmc"));
                        map.put("fdsx", r.get(i).getAttribute("fdsx"));
                        map.put("fdlx", r.get(i).getAttribute("fdlx"));
                        map.put("zdsxm", r.get(i).getAttribute("zdsxm"));
                        map.put("gdz", r.get(i).getAttribute("gdz"));
                        map.put("lshcd", r.get(i).getAttribute("lshcd"));
                        map.put("lshbc", r.get(i).getAttribute("lshbc"));
                        map.put("lshgs", r.get(i).getAttribute("lshgs"));
                        map.put("coderule_id", bmid.getValueAsString());
                        map.put("bb", (Integer.parseInt(bbField.getValueAsString()) + 1) + "");
                        listMap.add(map);

                    }

                    Map<String, String> data = new HashMap<>();
                    data.put("qz", record.getAttribute("qz"));
                    data.put("hz", record.getAttribute("hz"));
                    data.put("fgf", record.getAttribute("fgf"));


                    Map<String, String> map = new HashMap<>();
                    map.put("cglx", "dl");
                    map.put("mrp_lx", "xdl");
                    CodeRuleService.App.getInstance().getMessage(data, listMap, map, new AsyncCallback<String>() {
                        public void onSuccess(String result) {
                            SC.say("测试编码成功！ " + result);
                        }

                        public void onFailure(Throwable caught) {
                            SC.warn("RPC failed");
                        }
                    });
                }
            }
        });

        Button xybButton = new Button("下一步");
        xybButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                RecordList r = finalListGridAtter.getDataAsRecordList();
                if (r.getLength() < 1) {
                    SC.say("请编辑分段配置！");
                } else {

                    ListGridRecord bmgzrecord = new ListGridRecord();
                        bmgzrecord.setAttribute("id", bmid.getValueAsString());
                        bmgzrecord.setAttribute("db_model_id", db_model_idField.getValueAsString());
                        bmgzrecord.setAttribute("db_model_name", db_model_name.getValueAsString());

                        if(record==null){
                            bmgzrecord.setAttribute("mc", name + "编码规则");
                        }else{
                            bmgzrecord.setAttribute("mc", mcField.getDisplayValue());
                        }

                        bmgzrecord.setAttribute("ms", msField.getValueAsString());
                        bmgzrecord.setAttribute("fgf", fgfField.getValueAsString());
                        bmgzrecord.setAttribute("qz", qzField.getValueAsString());
                        bmgzrecord.setAttribute("hz", hzField.getValueAsString());
                        bmgzrecord.setAttribute("bb", bbField.getValueAsString());
                        bmgzrecord.setAttribute("bmgf", bmgfField.getValueAsString());
                        bmgzrecord.setAttribute("fbzt", fbzt.getValueAsString());
                        bmgzrecord.setAttribute("status", statusField.getValueAsString());


                    List<ListGridRecord> fdrecordList = new ArrayList<>();
                    for (int i = 0; i < r.getLength(); i++) {
                        ListGridRecord fdrecord = new ListGridRecord();
//                        LinkedHashMap<String, String> map = new LinkedHashMap<>();
//                        fdrecord.setAttribute("id", r.get(i).getAttribute("id"));
                        fdrecord.setAttribute("fdmc", r.get(i).getAttribute("fdmc"));
                        fdrecord.setAttribute("fdsx", r.get(i).getAttribute("fdsx"));
                        fdrecord.setAttribute("fdlx", r.get(i).getAttribute("fdlx"));
                        fdrecord.setAttribute("zdsxm", r.get(i).getAttribute("zdsxm"));
                        fdrecord.setAttribute("gdz", r.get(i).getAttribute("gdz"));
                        fdrecord.setAttribute("lshcd", r.get(i).getAttribute("lshcd"));
                        fdrecord.setAttribute("lshbc", r.get(i).getAttribute("lshbc"));
                        fdrecord.setAttribute("lshgs", r.get(i).getAttribute("lshgs"));
                        fdrecord.setAttribute("coderule_id", bmid.getValueAsString());
                        fdrecord.setAttribute("bb", bbField.getValueAsString());
//                        map.put("bb", (Integer.parseInt(bbField.getValueAsString()) + 1) + "");
                        fdrecordList.add(fdrecord);
                    }

                    if (type.equals("bj")) {
                        Window dialog = CodeRuleWindow.getShowWindow(0, 1000, 650, "(4/5)测试编码规则", bmgzrecord, listGrid, istrue, "", "", "", "cs",fdrecordList);
                        dialog.show();
                        winModal.hide();
                    } else if (type.equals("cs")) {
//                        type=="fb";
                        winModal.setTitle("(5/5)发布编码规则");
                        xybButton.setVisible(false);
                        hqbmImg.setVisible(false);
                        saveButton.setVisible(true);
                        fbButton.setVisible(true);
                        if(record.getAttribute("fbzt").equals("发布")){
                            sbButton.setVisible(true);
                            saveButton.setVisible(false);
                            fbButton.setVisible(false);
                        }
                    }

                }
            }
        });

        Button cancelButton = new Button("关闭");
        cancelButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                SC.ask("关闭操作不会进行数据保存！是否关闭?", new BooleanCallback() {
                    public void execute(Boolean value) {
                        if (value != null && value) {
                            winModal.hide();
                        } else {

                        }
                    }
                });
            }
        });

        Button sybButton = new Button("上一步");
        sybButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {

            if(type.equals("cs")){

                if(winModal.getTitle().equals("(5/5)发布编码规则")){
                    winModal.setTitle("(4/5)测试编码规则");
                    xybButton.setVisible(true);
                    hqbmImg.setVisible(true);
                    saveButton.setVisible(false);
                    fbButton.setVisible(false);
                    if(record.getAttribute("fbzt").equals("发布")){
                        sbButton.setVisible(false);
                    }
                }else{
                    Window dialog = CodeRuleWindow.getShowWindow(0, 1000, 650, "(3/5)编辑编码信息", record, listGrid, istrue, "", "", "", "bj",fdrecordList);
                    dialog.show();
                    winModal.hide();
                }

            }else if(type.equals("bj")){
                Window bmgf = CodingSpecificationwindow.getShowWindow(800,390,"(2/5)编辑编码规范",record,listGrid,"","");
                bmgf.draw();
                winModal.hide();
            }
            }
        });

        if (record != null) {
            form.setItems(db_model_name, mcField, msField, fgfField, qzField, hzField, bbField);
        } else {
            form.setItems(db_model_name, msField, fgfField, qzField, hzField, bbField);
        }

        winModal.addItem(form);

        HLayout buttonLayout = new HLayout();
        buttonLayout.setMembersMargin(10);
        buttonLayout.setAlign(Alignment.RIGHT);
        buttonLayout.setWidth100();
        buttonLayout.setHeight(30);
        buttonLayout.setPadding(5);
        buttonLayout.setLayoutAlign(VerticalAlignment.BOTTOM);

        layout.setWidth100();
        layout.setHeight100();
        winModal.addItem(layout);

        if(type.equals("cs")){
            hqbmImg.setVisible(true);
        }

        if(type.equals("xq")){
            hqbmImg.setVisible(true);
            buttonLayout.addMembers(hqbmImg, cancelButton);
        }else{
            buttonLayout.addMembers(hqbmImg,saveButton, fbButton,sybButton,xybButton,sbButton, cancelButton);
        }
        winModal.addItem(buttonLayout);

        HStack layoutTopRow = new HStack(10);
        layoutTopRow.setHeight(50);

        VLayout layoutMain = new VLayout();
        layoutMain.addMember(layoutTopRow);

        layoutMain.draw();
        return winModal;
    }

    public static Map<String, String> convertRecordToMap(Record record) {
        JavaScriptObject jsObj = record.getJsObj();
        return JSOHelper.convertToMap(jsObj);
    }

    public static String getuuid(int length) {

        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // 定义包含所有字母大小写的字符集合

        StringBuilder id = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            char c = characters.charAt(random.nextInt(characters.length())); // 从字符集合中随机选取一个字符

            id.append(c); // 将字符添加到字符串构建器中
        }
        return id.toString();
    }
}
